class Shim < ActiveRecord::Base
  
  SHIM_SERVICE_TYPES = ['REST Service', 'SOAP Service', 'SOAPLab Service',
                        'BioMart Service', 'BioMoby Service', 'Arbitrary WSDL']
                        
  SHIM_OTHER_TYPES = ['Beanshell', 'Taverna Local Worker', 'Workflow', 'Other']
  
  SHIM_TYPES = SHIM_SERVICE_TYPES | SHIM_OTHER_TYPES 
  
  SHIM_CLASSES = %w{Generic Convertor Mapper Parser Retriever Other} 

  acts_as_annotatable
  
  acts_as_solr :fields => [:name]
  
  validates_presence_of :name, :shim_type
  
  validates_inclusion_of :shim_type, :in => SHIM_TYPES
  
  validates_inclusion_of :shim_class, :in => SHIM_CLASSES, :allow_nil => true
  
  validates_uniqueness_of :shim_blob_id, :allow_nil => true
  
  validates_uniqueness_of :name
  
  validates_length_of :name, :minimum => 3
  
  validate :shim_blob_present
  
  validate :service_id_present
  
  belongs_to :shim_blob,
             :dependent => :destroy,
             :validate => :true
  
  belongs_to :user
  
  has_many :shim_inputs,
           :dependent => :destroy
           
  has_many :shim_outputs,
           :dependent => :destroy
  
  def Shim.all_shim_types
    temp = []
    temp << SHIM_SERVICE_TYPES
    temp << SHIM_OTHER_TYPES
    return temp.flatten
  end
  
  def is_service?
    SHIM_SERVICE_TYPES.include?(shim_type)
  end
  
  private
    
  def shim_blob_present
    errors.add_to_base("There was an error uploading your file") if shim_blob_id.nil? && SHIM_OTHER_TYPES.include?(shim_type)
  end

  def service_id_present
    if SHIM_SERVICE_TYPES.include?(shim_type)
      if service_id.nil?
        errors.add_to_base("There was an error creating the service")
      end
    end  
  end  
end
